/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sheep.view.Component;

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JPanel;

/**
 *
 * @author geek
 */
    public class RoundedPanel extends JPanel {
        private BufferedImage cache;

        public RoundedPanel(JComponent component) {
            super(new BorderLayout());
            setOpaque(false); 
            component.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            add(component);
        }

        public RoundedPanel(JComponent component, int top, int right, int bottom, int left) {
            super(new BorderLayout());
            setOpaque(false); 
            component.setBorder(BorderFactory.createEmptyBorder(top, right, bottom, left));
            add(component);
        }
        @Override
        protected void paintComponent(Graphics g) {
            if (cache == null || cache.getWidth() != getWidth() ||
                cache.getHeight() != getHeight()) {
                cache = new BufferedImage(getWidth(), getHeight(),
                    BufferedImage.TYPE_INT_ARGB);

                Graphics2D g2 = cache.createGraphics();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                    RenderingHints.VALUE_ANTIALIAS_ON);

                RoundRectangle2D rect = new RoundRectangle2D.Double(
                    0, 0, getWidth(), getHeight(), 12, 12);
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
                g2.setColor(Color.WHITE);
                g2.fill(rect);
            }

            g.drawImage(cache, 0, 0, null);
        }
    }